#作者：朱易，万茂洁

import pymysql
from selenium.webdriver import Chrome
from selenium.webdriver.common.by import By

url="https://data.eastmoney.com/"
def getData():
    web=Chrome()
    web.get(url)
    data=web.find_elements(by=By.XPATH,value='//*[@id="dpzjl_dt"]/tbody/tr')
    arr1 = []
    for td in data:
        arr1.append(td.text.split(" "))

        print(arr1)
    return arr1

def savaData(arr1):
    #连接数据库
    #创建链接
    conn = pymysql.connect(host='111.229.116.141',port=3306,user='yfzx',password='xf@yfzx',database='stock_strategy')
    #创建游标
    cursor = conn.cursor();

    for row in arr1:
        Type = row[0]
        input = row[1]
        output = row[2]
        netInflow = row[3]
        netShare = row[4]


        print(Type,input,output,netInflow,netShare)
            #保存到数据库
        params = [Type,input,output,netInflow,netShare]
        sql = 'insert into money_flow_shenhu (Type,input,output,netInflow,netShare) values (%s,%s,%s,%s,%s)'

        cursor.execute(sql,params);
        #提交
        conn.commit()
    conn.close()

if __name__ == '__main__':
    data = getData()
    savaData(data)






